package com.SNQ.project.English_speaking_learning_system.controller.admin;

import com.SNQ.project.English_speaking_learning_system.common.result.PageResult;
import com.SNQ.project.English_speaking_learning_system.common.result.Result;
import com.SNQ.project.English_speaking_learning_system.service.ArticleService;
import com.SNQ.project.English_speaking_learning_system.pojo.dto.ArticleDTO;
import com.SNQ.project.English_speaking_learning_system.pojo.entity.Article;
import com.SNQ.project.English_speaking_learning_system.pojo.dto.ArticlePageQueryDTO;
import com.SNQ.project.English_speaking_learning_system.pojo.vo.ArticleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/article")
@Api(tags = "文章相关接口")
@Slf4j
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 创建文章
     * @param articleDTO
     * @return
     */
    @PostMapping
    @ApiOperation("创建文章")
    public Result createArticle(@RequestBody ArticleDTO articleDTO) throws Exception {
        log.info("创建文章：{}",articleDTO);
        articleService.createArticle(articleDTO);

//        //清理缓存数据
//        String key = "dish" + dishDTO.getCategoryId();
//        cleanCache(key);

        return Result.success();
    }

    /**
     * 文章分页查询
     * @param articlePageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("文章分页查询")
    public Result<PageResult> page(ArticlePageQueryDTO articlePageQueryDTO){
        log.info("文章分页查询：{}",articlePageQueryDTO);
        PageResult pageResult = articleService.pageQuery(articlePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 文章批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("文章批量删除")
    public Result delete(@RequestParam List<Long> ids){
        log.info("文章批量删除：{}",ids);
        articleService.deleteBatch(ids);

//        //将所有的菜品缓存数据清理掉，所有以dish_开头的key
//        cleanCache("dish*");

        return Result.success();
    }

    /**
     * 根据id查询文章
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询文章")
    public Result<ArticleVO> getById(@PathVariable Long id){
        log.info("根据id查询文章：{}",id);
        ArticleVO articleVO = articleService.getById(id);
        return Result.success(articleVO);
    }

    /**
     * 修改文章
     * @param articleDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改文章信息")
    public Result update(@RequestBody ArticleDTO articleDTO) {
        log.info("修改文章信息, {}", articleDTO);
        articleService.update(articleDTO);

//        // 将所有的菜品缓存都删除(以dish_开头的)
//        clearCache("dish_*");
        return Result.success();
    }

    /**
     * 根据分类id查询文章
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询文章")
    public Result<List<Article>> list(Long categoryId) {
        List<Article> list = articleService.list(categoryId);
        return Result.success(list);
    }




//    /**
//     * 清理缓存数据
//     *
//     * @param pattern
//     */
//    private void clearCache(String pattern) {
//        // 将所有的菜品缓存都删除(以dish_开头的)
//        Set keys = redisTemplate.keys(pattern);
//        redisTemplate.delete(keys);
//    }
}
